//
//  CYLMainRootViewController.m
//  CYLTabBarController
//
//  Created by chenyilong on 7/3/2019.
//  Copyright © 2019 微博@iOS程序犭袁. All rights reserved.
//

#import "CYLMainRootViewController.h"
#import "MainTabBarController.h"
#import "CYLPlusButtonSubclass.h"
#import "MTOkTishiView.h"
#import "MTShouhuView.h"
#import "MTHomeVC.h"



#if __has_include(<Lottie/Lottie.h>)
#import <Lottie/Lottie.h>
#else
#endif

#define RANDOM_COLOR [UIColor colorWithHue: (arc4random() % 256 / 256.0) saturation:((arc4random()% 128 / 256.0 ) + 0.5) brightness:(( arc4random() % 128 / 256.0 ) + 0.5) alpha:1]

@interface CYLMainRootViewController ()<UITabBarControllerDelegate, CYLTabBarControllerDelegate>

@property (nonatomic, strong) MainTabBarController *tabBarController;

@end

@implementation CYLMainRootViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationBarHidden = YES;
    self.view.backgroundColor = [UIColor whiteColor];
    [CYLPlusButtonSubclass registerPlusButton];
    [self createNewTabBar];

    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(selectIndex) name:MTFabuNotification1 object:nil ];
}
- (void)selectIndex {
//    [self.tabBarController setSelectedIndex:1] ;
//    = 4;
//    self.tabBarController.selectedIndex = 4;
}
- (void)createNewTabBar {
    self.tabBarController = [[MainTabBarController alloc] init];
    self.tabBarController.delegate = self;
    self.viewControllers = @[self.tabBarController];
}

//缩放动画
- (void)addOnceScaleAnimationOnView:(UIView *)animationView {
    //需要实现的帧动画，这里根据需求自定义
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animation];
    animation.keyPath = @"transform.scale";
    animation.values = @[@0.5, @1.0];
    animation.duration = 0.1;
    //    animation.repeatCount = repeatCount;
    animation.calculationMode = kCAAnimationCubic;
    [animationView.layer addAnimation:animation forKey:nil];
}


#pragma mark - delegate
- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
    BOOL should = YES;
    [[self cyl_tabBarController] updateSelectionStatusIfNeededForTabBarController:tabBarController shouldSelectViewController:viewController shouldSelect:should];

    if ([viewController isKindOfClass:[MTHomeVC class]]) {
//        [self.view makeToast:@"选择了第二个"];
        //只创建一次
        NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];
        NSString *fabu = [userDefault objectForKey:@"fabu"];
        if ([fabu isEqualToString:@"1"]) {
            if (tabBarController.selectedIndex == 0) {
                [[NSNotificationCenter defaultCenter] postNotificationName:MTCardNotification1 object:nil];
            }else if (tabBarController.selectedIndex == 1) {
                [[NSNotificationCenter defaultCenter] postNotificationName:MTCardNotification2 object:nil];
            }else if (tabBarController.selectedIndex == 2) {
                [[NSNotificationCenter defaultCenter] postNotificationName:MTCardNotification3 object:nil];
            }else if (tabBarController.selectedIndex == 4) {
                [[NSNotificationCenter defaultCenter] postNotificationName:MTCardNotification4 object:nil];
            }
        }else {
//            NSUserDefaults *dataUser = [NSUserDefaults standardUserDefaults];
//            [dataUser setObject:@"1" forKey:@"fabu"];
//            [dataUser synchronize];
            
            MTShouhuView *okView = [[MTShouhuView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight)];


            okView.bgView.backgroundColor = KMainColor;
            okView.goButClick = ^(NSString *  s) {
                if (tabBarController.selectedIndex == 0) {
                    [[NSNotificationCenter defaultCenter] postNotificationName:MTCardNotification1 object:nil];
                }else if (tabBarController.selectedIndex == 1) {
                    [[NSNotificationCenter defaultCenter] postNotificationName:MTCardNotification2 object:nil];
                }else if (tabBarController.selectedIndex == 2) {
                    [[NSNotificationCenter defaultCenter] postNotificationName:MTCardNotification3 object:nil];
                }else if (tabBarController.selectedIndex == 4) {
                    [[NSNotificationCenter defaultCenter] postNotificationName:MTCardNotification4 object:nil];
                }
            };
            [[UIApplication sharedApplication].keyWindow addSubview:okView];
        }
        
       
        
        return NO;
    }
    return should;
 }

- (void)tabBarController:(UITabBarController *)tabBarController didSelectControl:(UIControl *)control {
    
    UIView *animationView;
    if ([control cyl_isTabButton]) {
        //更改红标状态
        animationView = [control cyl_tabImageView];
    }
//    [self addScaleAnimationOnView:animationView repeatCount:1];

}

//缩放动画
- (void)addScaleAnimationOnView:(UIView *)animationView repeatCount:(float)repeatCount {
    //需要实现的帧动画，这里根据需求自定义
    CAKeyframeAnimation *animation = [CAKeyframeAnimation animation];
    animation.keyPath = @"transform.scale";
    animation.values = @[@1.0,@1.3,@0.9,@1.15,@0.95,@1.02,@1.0];
    animation.duration = 1;
    animation.repeatCount = repeatCount;
    animation.calculationMode = kCAAnimationCubic;
    [animationView.layer addAnimation:animation forKey:nil];
}

//旋转动画
- (void)addRotateAnimationOnView:(UIView *)animationView {
    // 针对旋转动画，需要将旋转轴向屏幕外侧平移，最大图片宽度的一半
    // 否则背景与按钮图片处于同一层次，当按钮图片旋转时，转轴就在背景图上，动画时会有一部分在背景图之下。
    // 动画结束后复位
    animationView.layer.zPosition = 65.f / 2;
    [UIView animateWithDuration:0.32 delay:0 options:UIViewAnimationOptionCurveEaseIn animations:^{
        animationView.layer.transform = CATransform3DMakeRotation(M_PI, 0, 1, 0);
    } completion:nil];
    
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [UIView animateWithDuration:0.70 delay:0 usingSpringWithDamping:1 initialSpringVelocity:0.2 options:UIViewAnimationOptionCurveEaseOut animations:^{
            animationView.layer.transform = CATransform3DMakeRotation(2 * M_PI, 0, 1, 0);
        } completion:nil];
    });
}
@end
